﻿using B.S.Data.MES.API.Applications.AccountApplication.Command;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

namespace B.S.Data.MES.API.Applications.AccountApplication.CommandHandler
{
    /// <summary>
    /// 实现删除命令
    /// </summary>
    public class AccountDelCommandHandler : IRequestHandler<AccountDelCommand, APIRequst<int>>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountRepository">用户表</param>
        public AccountDelCommandHandler(IBaseRepository<Account> accountRepository)
        {
            AccountRepository = accountRepository;
        }
        /// <summary>
        /// 用户表
        /// </summary>
        public IBaseRepository<Account> AccountRepository { get; }
        /// <summary>
        /// 处理删除命令
        /// </summary>
        /// <param name="request">命令</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns></returns>
        public async Task<APIRequst<int>> Handle(AccountDelCommand request, CancellationToken cancellationToken)
        {
            var path = AccountRepository.GetAll().FirstOrDefault(a => a.UserId == request.Id);
            path.isDel = true;
            await AccountRepository.Update(path);
            return new APIRequst<int>
            {
                Code = RequstCode.成功,
                Msg = "删除成功",
                Token="",
                Data = 1
            };
        }
    }
}
